from models import *



def add(req):
    try: 
        title = req['title']
        content = req['content']
        
        category = req['category']
        mes = MessageModel(title=title)
        mes.category = category
        mes.content = content
        mes.modifiedtime = datetime.datetime.now()
        mes.save()
        return True
    except:
        return False

def getone(rid):
    try:
        mes = MessageModel.objects(id=rid)
        if mes.count():
            return mes[0].to_json()
        else:
            return None
    except:
        return False
    
def getAll():
    try:
        mes =MessageModel.objects
        if mes.count():
            return mes.to_json()
        else:
            return None
    except:
        return False
    
def deleteone(rid):
    try:
        mes =MessageModel.objects(id=rid)
        if mes():
            mes.delete()
            return True
        else:
            return False
    except:
        return False
    

def getThen(rid):
#     filter messages which id is later than 'rid'
    try:
        mes = MessageModel.objects(id__gt=rid)
        if mes.count():
            return mes.to_json()
        else:
            return None
    except:
        return False

